package com.gkzf.ai.module.crm.util.assembly;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gkzf.ai.module.crm.controller.admin.creditReport.vo.creditreportdetails.CreditReportDetailsSaveReqVO;
import com.gkzf.ai.module.crm.dal.dataobject.creditReport.CreditReportCreditCardDetailsDO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.gkzf.ai.module.crm.util.AnalysisJsonObjectUtils.convertMoney;

@Slf4j
public class CreditAccountDetails {

    private static final String  OUTSTANDING_LOAN_ACCOUNT_DETAILS = "未结清账户明细列表";

    public static final String CARD_ISSUER = "发卡机构";

    public static final String CREDIT_LINE = "授信额度";

    public static final String USED_CREDIT = "已用额度";

    public static final String BALANCE = "余额";

    public static final String ACCOUNT_STATUS = "账户状态";

    /**
     *  未结清贷款账户明细
     * @param param 参数Map
     * @param creditReportDetailsSaveReqVO 对象
     * @return
     */
    public void getCreditAccountDetails(Map<String, Object> param, CreditReportDetailsSaveReqVO creditReportDetailsSaveReqVO) {
        List<CreditReportCreditCardDetailsDO> creditReportCreditCardDetailsList = new ArrayList<>();
        Object loanAccountList = param.get(OUTSTANDING_LOAN_ACCOUNT_DETAILS);
        if (loanAccountList instanceof JSONArray loanAccount) {
            for (Object loanAccountObj : loanAccount) {
                CreditReportCreditCardDetailsDO creditReportLoanOverdueDO = new CreditReportCreditCardDetailsDO();
                JSONObject overdueDetailsMap = (JSONObject) loanAccountObj;
                creditReportLoanOverdueDO.setHairpinInstitutionName(AssembleUtils.getStringValue(overdueDetailsMap, CARD_ISSUER));
                creditReportLoanOverdueDO.setCreditLine(AssembleUtils.getStringValue(overdueDetailsMap, CREDIT_LINE));
                creditReportLoanOverdueDO.setUsedQuota(AssembleUtils.getStringValue(overdueDetailsMap, USED_CREDIT));
                creditReportLoanOverdueDO.setBalance(AssembleUtils.getStringValue(overdueDetailsMap, BALANCE));
                creditReportLoanOverdueDO.setAccountStatus(AssembleUtils.getStringValue(overdueDetailsMap, ACCOUNT_STATUS));
//                if (creditReportDetailsSaveReqVO.getCreditCardUsedCredit().compareTo(BigDecimal.ZERO) > 0){
//                    // 月还款去余额的百分之10
//                    creditReportLoanOverdueDO.setMonthlyRepayment(String.valueOf(creditReportDetailsSaveReqVO.getCreditCardUsedCredit().multiply(BigDecimal.valueOf(0.1))));
//                }
                creditReportCreditCardDetailsList.add(creditReportLoanOverdueDO);
            }
            creditReportDetailsSaveReqVO.setLoanOverdueCreditCardDetailsList(creditReportCreditCardDetailsList);
        }
    }

    /**
     * 截取金额中的中文字
     * @param usedQuota 已用额度
     * @return
     */
    public Integer convertBalance(String usedQuota){
        if (StringUtils.isEmpty(usedQuota)) {
            log.info("信用贷款月还款转换余额为空");
            return 0;
        }
        // 匹配一个或多个数字 (0-9)
        Pattern pattern = Pattern.compile("\\d+");
        Matcher matcher = pattern.matcher(usedQuota);

        if (matcher.find()) {
            String numberString = matcher.group();
            log.info("信用贷款月还款转换余额: " + numberString);

            // 如果需要转换为整数或其他数字类型
            try {
                int number = Integer.parseInt(convertMoney(numberString.trim()));
                log.info("信用贷款月还款转换余额转换为整数: " + number);
                return number;
            } catch (NumberFormatException e) {
                log.info("信用贷款月还款转换余额无法将截取的字符串转换为整数: " + numberString);
                return 0;
            }
        } else {
            log.info("信用贷款月还款转换余额未找到数字");
            return 0;
        }
    }

}
